/************************************************************************/
/* Game                                                                 */
/************************************************************************/
#include "Game/Animations/Animation.h"

namespace TD_Game {

	/************************************************************************/
	/* Default values                                                       */
	/************************************************************************/
	const unsigned int Animation::defaultAnimationInterval = 100;
	const string Animation::noAnimation = "_NO_ANIMATION_";

	/************************************************************************/
	/* ctors and dtors                                                      */
	/************************************************************************/
	//Default constructor
	Animation::Animation() : BaseEntity () {
		m_pCurrTile = NULL;
		m_name = noAnimation;
		m_animationInterval = defaultAnimationInterval;
		m_loopAnimation = true;
	}
	//Copy constructor
	Animation::Animation(const Animation &rhs) : BaseEntity (rhs) {
		m_name = rhs.getName();
		m_loopAnimation = rhs.isLoopedAnimation();
		m_tileVector = rhs.getTileVector();
		m_pCurrTile = rhs.getCurrentTile();
		m_animationInterval = rhs.getAnimationInterval();
	}
	//Assignement constructor
	Animation & Animation::operator=(const Animation &rhs) {
		m_name = rhs.getName();
		m_loopAnimation = rhs.isLoopedAnimation();
		m_tileVector = rhs.getTileVector();
		m_pCurrTile = rhs.getCurrentTile();
		m_animationInterval = rhs.getAnimationInterval();
		return(*this);
	}

	//Initialization
	Animation::Animation(const string &animationName, int animationInterval, bool loopAnimation /* = true */, const Coord2D & position) 
		: BaseEntity(position) {

		m_name = animationName;
		m_animationInterval = animationInterval;
		m_loopAnimation = loopAnimation;

		m_animationComplete = false;
		m_firstRenderLoop = true;

		m_animationTicks = 0;
		m_currAnimIndex = 0;

		m_pCurrTile = NULL;
	}

	Animation::~Animation(){
		/************************************************************************/
		/* DOES NOTHING                                                         */
		/************************************************************************/
		//The ownership is of the tile Vector is of the tileset.
		m_pCurrTile = NULL;
	}

	/************************************************************************/
	/* Private functions                                                    */
	/************************************************************************/
	void Animation::update(unsigned int deltaTime) {
		if(m_firstRenderLoop) {
			if(m_tileVector.size() > 0) {
				//Initialization of the current tile
				m_pCurrTile = m_tileVector.at(0);
				m_firstRenderLoop = false;
			}
		} else {
			m_animationTicks += deltaTime;
			if(m_animationInterval - m_animationTicks <= 0) {
				if(m_currAnimIndex >= m_tileVector.size()) { //If the animation has finished
					if(m_loopAnimation) { //Is it looped?
						m_currAnimIndex = 0; //Restart
					} else {
						m_animationComplete = true; //Finished
					}
				} else {
					m_pCurrTile = m_tileVector[m_currAnimIndex];
					m_animationTicks = 0;
					m_currAnimIndex++;
				}
			}
		}
	}

	/************************************************************************/
	/* Functionalities                                                     */
	/************************************************************************/
	void Animation::resetAnimation() {
		m_animationComplete = false;
		m_firstRenderLoop = true;

		m_animationTicks = 0;
		m_currAnimIndex = 0;
	}

	/************************************************************************/
	/* Rendering                                                            */
	/************************************************************************/
	void Animation::render(SDL_Surface *const g_destSurface, unsigned int deltaTime) {
		if(!m_animationComplete) {
			update(deltaTime);
			if(m_pCurrTile != NULL) {
				m_pCurrTile->setPosition(m_position - m_pCurrTile->getTileSize()/2);
				m_pCurrTile->render(g_destSurface, deltaTime);
			}
		}
	}
}